package zhklappoint.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import zhklappoint.dao.FunctionAndCompanyDao;
import zhklappoint.entity.FunctionAndCompany;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.service.FunctionAndCompanyService;
import zhklappoint.util.PageUtils;

@Service
public class FunctionAndComapnyServiceImpl implements FunctionAndCompanyService{

	@Autowired
	private FunctionAndCompanyDao facDao;
	
	@Override
	public FunctionAndCompany saveFunctionAndCompanyObj(FunctionAndCompany fkCom) {
		
		return facDao.save(fkCom);
	}

	@Override
	public Page<FunctionAndCompany> getFunctionAndCompanyObj(FunctionAndCompany fkCom, Integer page, Integer size) {
		Pageable pageable = PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("isdelete", ENUM_ISDELETE.NOT_DELETED.value);
		StringBuffer queryString = new StringBuffer("select * from zhkl_function_company ");
		queryString.append(" WHERE isdelete = :isdelete ");
		if(!ObjectUtils.isEmpty(fkCom.getFkId())) {
			values.put("fkId", fkCom.getFkId());
			queryString.append(" and fk_id = :fkId");
		}
		if(!ObjectUtils.isEmpty(fkCom.getCompanyId())) {
			values.put("comId", fkCom.getCompanyId());
			queryString.append(" and company_id = :comId");
		}
		
			if(!ObjectUtils.isEmpty(fkCom.getStatus())) {
					values.put("status", fkCom.getStatus());
					queryString.append(" and status = :status");
			}
		if(!ObjectUtils.isEmpty(fkCom.getBeginTime())) {
			values.put("begTime", fkCom.getBeginTime());
			queryString.append(" and begin_time > :begTime");
		}
		if(!ObjectUtils.isEmpty(fkCom.getEndTime())) {
			values.put("endTime", fkCom.getEndTime());
			queryString.append(" and end_time < :endTime");
		}
		queryString.append(" ORDER BY status ASC,createdate DESC ");
		StringBuffer countString = new StringBuffer("select count(1) from (").append(queryString).append(") a");
		return facDao.sqlQuery(queryString+"", countString+"", values, pageable,fkCom.getClass());
	}
	
}
